package com.szcinda.call.service;

import com.szcinda.call.Result;
import com.szcinda.call.repository.SiteUser;
import com.szcinda.call.repository.SiteUserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.time.LocalDate;

@Service
@Transactional
public class SiteUserServiceImpl implements SiteUserService {
    private final SiteUserRepository siteUserRepository;

    public SiteUserServiceImpl(SiteUserRepository siteUserRepository) {
        this.siteUserRepository = siteUserRepository;
    }

    @Override
    public Result<String> login(LoginDto loginDto) {
        SiteUser siteUser = siteUserRepository.findByUserName(loginDto.getUserName());
        Assert.isTrue(siteUser.getPassword().equals(loginDto.getPassword()), "密码不正确");
        Assert.isTrue(LocalDate.now().isBefore(siteUser.getExpireDate()), "账号已过期");
        return Result.success();
    }

    @Override
    public Result<String> check(String userName) {
        SiteUser siteUser = siteUserRepository.findByUserName(userName);
        Assert.isTrue(siteUser.getCount() > 0, "点数不够,请重置");
        return Result.success();
    }
}
